/**
 * Created by lvliqi on 2016/7/12.
 */

/*
 记录我创 跑步机
 扫码
 2个
 点击开始
 丫丫
 */

var express = require("express");
var router = express.Router();
var status_enum = require("../status_enum");

var activity_enum = require("./activity_enum");

var activityId = activity_enum.GUINNESS_RECORDS;


router.post("/check", function (req, res) {
    var Queue = M("index.queue");
    var returnData = 0;
    Queue.find({
        where: {
            activityId: activityId,
            status: status_enum.PLAY_BTN
        }
    }).then(function (data) {
        if (!data) throw new Error("no data");
        returnData = data;
        return Queue.update({
            status: status_enum.PLAYING
        }, {
            where: {
                id: data.id
            }
        });
    }).then(function () {
        var b = new Buffer(returnData.username, 'base64');
        var username = encodeURIComponent(b.toString());

        res.json({
            errno: 0,
            data: {
                player: returnData.player,
                username: username
            }
        });
    }).catch(function (err) {
        res.json({
            errno: 10001,
            errdesc: err.message
        });
    });
});

module.exports = router;